#ifndef _DEC_AUDIO_H_
#define _DEC_AUDIO_H_

extern "C" {
#include <libavutil/frame.h>
#include <libavutil/mem.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include "libswresample/swresample.h"
#include "libswscale/swscale.h"
}

#include <string>
#include <map>
#include "ring_buffer_bytes.h"

class FmtCvt;
class AudioDecode
{
public:
    AudioDecode(int out_sample_rate = 16000);
    ~AudioDecode();

    int start();
    int stop();
    int destroy();

    int decode(uint8_t** out_pptr);
    int send_data(const uint8_t* data, int data_size);

private:
    int init();
    int _decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame);
    int _decode_old(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame);

private:
    AVCodec* m_codec;
    AVCodecContext* m_codec_ctx;
    AVFormatContext *m_fmt_ctx;
    AVPacket* m_pkt;
    AVFrame* m_frm;
    FmtCvt* m_swr;
    uint8_t* m_buffer;
    int m_buffer_size;
    int m_buffer_capacity;
    bool m_is_inited;
    unsigned char *m_avio_buffer;
    int m_audio_stream_idx;
    bool m_is_end;
    //std::map<const std::string, AVCodecID> m_codec_map;

public:
    RingBufferBytes* m_ring;
};

#endif
